package com.jier.controller.api;

import com.jier.common.Result;
import com.jier.common.exception.HttpParamException;
import com.jier.common.util.StringUtil;
import com.jier.controller.BaseController;
import com.jier.entities.User;
import com.jier.model.PageParam;
import com.jier.service.IUserService;
import com.jier.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

import static com.jier.common.util.PropertiesUtil.getResStr;

/**
 * 用户控制类
 *
 * @author Administrator
 * @date 2016/8/22
 */
@RestController
@RequestMapping("/api/users/")
@Api(description = "用户")
public class UserApiCtrl extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 获取用户信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET, produces = "application/json;")
    @ApiOperation(value = "获取用户信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<UserVo> loadUserById(@PathVariable Integer id) throws Exception {
        User user = userService.getById(id);
        return ok(new UserVo(user));
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, produces = "application/json;")
    @ApiOperation(value = "添加用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Boolean> insertUser(User user) throws Exception {
        userService.save(user);
        return created(Boolean.TRUE);
    }

    /**
     * 分页获取用户信息
     *
     * @param keyword
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.GET, produces = "application/json;")
    @ApiOperation(value = "分页获取用户信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Result<UserVo>> loadUsersByPage(String keyword, PageParam pageParam) throws Exception {
        if (StringUtil.isEmpty(keyword)) {
            throw new HttpParamException("搜索值不能为空");
        }
        Result<UserVo> result = userService.loadUsersByPage(keyword, getPage(pageParam));
        return ok(result);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE, produces = "application/json;")
    @ApiOperation(value = "删除用户", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Boolean> deleteUser(@PathVariable String id) throws Exception {
        userService.deleteById(id);
        return noContent(Boolean.TRUE);
    }

    /**
     * 登陆
     *
     * @param id
     * @param password
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;")
    @ApiOperation(value = "登陆", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Boolean> login(@RequestParam("id") Integer id, @RequestParam("password") String password, HttpSession session) throws Exception {
        User user1 = userService.getById(id);
        if (user1 == null) {
            throw new HttpParamException(getResStr("user_not_find"));
        }
        User user2 = userService.login(id, password);
        if (user2 == null) {
            throw new HttpParamException(getResStr("password_error"));
        }
        session.setAttribute("currentUser", user2);
        return ok(Boolean.TRUE);
    }

    /**
     * 更新用户信息
     *
     * @param id
     * @param name
     * @param age
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PATCH, produces = "application/json;")
    @ApiOperation(value = "更新用户信息", httpMethod = "PATCH", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Boolean> updateUser(@PathVariable Integer id, String name, String age) throws Exception {
        User user = userService.getById(id);
        System.out.println(user.getVersion());
        boolean flag = userService.updateById(user) > 0;
        return created(flag);
    }

    /**
     * 页面跳转
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "browser/toLogin", method = RequestMethod.GET, produces = "text/html")
    @ApiOperation(value = "页面跳转", httpMethod = "GET", hidden = true, produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView toLoginPage(HttpSession session) throws Exception {
        ModelAndView mv = new ModelAndView();
        session.removeAttribute("currentUser");
        mv.setViewName("redirect:/view/login.jsp");
        return mv;
    }
}
